<?php
/**
 * 邮件控制类
 * @author tangbl
 *
 */
class EmailAction extends CommonAction {
	
	public function _initialize() {
		parent::_initialize();
		parent::_initmenu();	
	}
	
	/**
	 * 发送测试邮件
	 */
	public function email_test(){
		
		$email = $this->_get("email");
		$map   = array("email"=>$email,"content"=>"{%em_ceshi_email}","title"=>"{%em_ceshi_title}");
		$json  = sendRest("Email/single",$map);
		$this->success($json['info']);
	}
	
	/**
	 * 发送邮件   
	 */ 
	public function email_send(){
		
		if(IS_POST){
			
			$model = D("EmailSend");
			if($model->create()){
				
				$map = $model->data();
				$map['addid'] = session(C("USER_AUTH_KEY"));
				if(empty($map['table']) && empty($map['email'])){
					$this->error(L("eml_send_email_reuire"));
				}
				$json = sendRest("Email/send",$map,"post");
				$this->success($json['info'],U("Email/email_send_list"));
			}else{
				$this->error($model->getError());
			}
			
		}elseif(IS_AJAX){
			
			$model  = D("EmailTemp");
			$lang   = $this->_param("lang","",getLang());
			$tempid = $this->_param("tempid","intval",0);
			
			$content= $model->getTempContent($tempid,$lang);
			$this->ajaxReturn($content);
			
		}else{
			
			$lang   = $this->_param("lang","",getLang());
			$lang   = create_option_key(C("LANG_ARRAY"),$lang);
			$this->assign("lang",$lang);
			
			$model  = D("EmailTemp");
			$list   = $model->getTempList();
			$temp   = create_option_array($list,"id","title");
			$this->assign("temp",$temp);
			
			$this->display();
		}
	}
	
	/**
	 * 邮件发送记录
	 */
	 public function email_send_list(){
		
	 	if(IS_AJAX){
	 	
	 		import('ORG.Util.Page');
	 		$model     = M("EmailSend");
	 		$orderDesc = $this->_get("orderDirection","trim","desc");
	 		$orderName = $this->_get("orderName","trim","addtime");
	 		$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
	 			
	 		$map       = unescape($this->_get("where"));
	 		$count     = $model->where($map)->count();
	 		$Page      = new Page($count);
	 		$nowPage   = $this->_get("p","intval",1);
	 		$list      = $model->where($map)->order($orderName.' '.$orderDesc)
	 					 ->page($nowPage.','.$pageNum)->select();
	 		
	 		$totalPage = ceil($count/$pageNum);
	 		C('web_admin_pagenum',$pageNum);
	 		$view      = L('eml_send_list_log');
	 		$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
	 		foreach($list AS $row){
	 			$entry = array('id'=>$row['id'],
	 				'cell'=>array(
	 					'id'      => $row['id'],
	 					'addtime' => date('Y-m-d H:i:s',$row['addtime']),
	 					'addid'   => $row['addid'],
	 					'send'    => $row['send'],
	 					'succ'    => $row['succ'],
	 					'fail'    => $row['fail'],
	 					'deal'    => '
 						 <a href="'.__URL__.'/email_send_log/id/'.$row['solevar'].'" class="btn blue icn-only" title="'.$view.'"><i class="icon-list"></i></a>'
	 				),
	 			);
	 			$jsonData['rows'][] = $entry;
	 		}
	 		$this->ajaxReturn($jsonData,'JSON');
	 	}else{
	 		$this->display();
	 	}
	}
	
	/**
	 * 邮件发送记录详情
	 */
	public function email_send_log(){
		
		if(IS_AJAX){
			 
			import('ORG.Util.Page');
			$model     = M("EmailSendLog");
			$orderDesc = $this->_get("orderDirection","trim","desc");
			$orderName = $this->_get("orderName","trim","addtime");
			$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));

			$map       = unescape($this->_get("where"));
			$count     = $model->where($map)->count();
			$Page      = new Page($count);
			$nowPage   = $this->_get("p","intval",1);
			$list      = $model->where($map)->order($orderName.' '.$orderDesc)
						 ->page($nowPage.','.$pageNum)->select();
		
			$totalPage = ceil($count/$pageNum);
			C('web_admin_pagenum',$pageNum);
			$view      = L('eml_send_list_log');
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
			foreach($list AS $row){
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'      => $row['id'],
						'addtime' => date('Y-m-d H:i:s',$row['addtime']),
						'feed'    => $row['feed'],
						'status'  => $row['status'],
						'email'   => $row['email']
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,'JSON');
		}else{
			
			$this->assign("solevar",$this->_get("id"));
			$this->display();
		}
	}
	
	/**
	 * 邮件模板列表 
	 */
	 public function email_temp(){
	 	
	 	if(IS_AJAX){
	 		
	 		import('ORG.Util.Page');
	 		$model     = M("EmailTemp");
	 		$orderDesc = $this->_get("orderDirection","trim","desc");
	 		$orderName = $this->_get("orderName","trim","addtime");
	 		$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
	 		$nowPage   = $this->_get("p","intval",1);
	 			
	 		$map       = unescape($this->_get("where"));
	 		$map       = "isdel=2 ".$map;
	 		$count     = $model->where($map)->count();
	 		$Page      = new Page($count);
	 		$list      = $model->where($map)->order($orderName.' '.$orderDesc)
	 					 ->page($nowPage.','.$pageNum)->select();
	 		$totalPage = ceil($count/$pageNum);
	 		C('web_admin_pagenum',$pageNum);
			
	 		$del = L('cfg_list_itf_btn_delete');
	 		$upd = L('cfg_list_itf_btn_modify');
	 		$lan = getLang();
	 		
	 		$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
	 		foreach($list AS $row){
	 			$entry = array('id'=>$row['id'],
 					'cell'=>array(
 						'id'      => $row['id'],
 						'sign'    => $row['sign'],
 						'title'   => dejsonByLang($row['title'],$lan),
 						'addtime' => date('Y-m-d',$row['addtime']),
 						'deal'    => '
 					<a href="#" class="btn red icn-only delAlone" title="'.$del.'" data-id="'.$row['id'].'"><i class="icon-remove"></i></a> 
 					<a href="'.__URL__.'/email_temp_edit/id/'.$row['id'].'" class="btn blue icn-only" title="'.$upd.'"><i class="icon-edit"></i></a>'
 					),
	 			);
	 			$jsonData['rows'][] = $entry;
	 		}
	 		$this->ajaxReturn($jsonData,'JSON');
	 	}else{
	 		$this->display();
	 	}
	 }
	
	/**
	 * 添加邮件模板 
	 */
	 public function email_temp_add(){
	 	
	 	$langs = C("LANG_ARRAY");
	 	
	 	if(IS_POST){
	 		
	 		$value = $this->_post("value");
	 		$json  = json_encode($value);
	 		$title = $this->_post("title");
	 		$title = json_encode($title);
	 		
	 		$param = array(
 				"reply" => false,
	 			"url"   => array("url"=>"Email/email_temp"),
	 			"arr"   => array("content"=>$json,"title"=>$title)
	 		);
	 		parent::do_add("EmailTemp",$param);
	 	}else{
	 		$this->assign("langs",$langs);
	 		$this->display();
	 	}
	 }
	 
	/**
	 * 修改邮件模板
	 */ 
	 public function email_temp_edit(){
	 	
	 	if(IS_POST){
	 		
	 		$value = $this->_post("value");
	 		$json  = json_encode($value);
	 		$title = $this->_post("title");
	 		$title = json_encode($title);
	 		
	 		$param = array(
 				"reply" => false,
	 			"url"   => array("url"=>"Email/email_temp"),
	 			"arr"   => array("content"=>$json,"title"=>$title)
	 		);
	 		parent::do_edit("EmailTemp",$param);
	 	}else{
	 			
	 		$data   = D("EmailTemp");
	 		$id     = $this->_get("id","intval",0);
	 		$row    = $data->find($id);
	 			
	 		$value  = json_decode($row['content'],true);
	 		$title  = json_decode($row['title'],true);
	 		$langs  = C("LANG_ARRAY");
	 		foreach($langs as $k=>$v){
	 			$values[$k] = array("title"=>$v,"value"=>$value[$k]);
	 			$titles[$k] = array("title"=>$v,"value"=>$title[$k]);
	 		}
	 		$this->assign("titles",$titles);
	 		$this->assign("langs",$values);
	 		$this->assign("detail",$row);
	 		$this->display();
	 	}
	 }
	 
	/**
	 * 删除邮件模板
	 * 物理删除
	 */ 
	 public function email_temp_del(){
	 	
	 	parent::do_del("EmailTemp",false);
	 }
	
	/**
	 * 订阅者列表  
	 */ 
	 public function email_sub(){
	 	
	 	if(IS_AJAX){
	 	
	 		import('ORG.Util.Page');
	 		$model     = M("EmailSubscribe");
	 		$orderDesc = $this->_get("orderDirection","trim","desc");
	 		$orderName = $this->_get("orderName","trim","addtime");
	 		$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
	 		$nowPage   = $this->_get("p","intval",1);
	 			
	 		$map       = unescape($this->_get("where"));
	 		$map       = "1=1 ".$map;
	 		$count     = $model->where($map)->count();
	 		$Page      = new Page($count);
	 		$list      = $model->where($map)->order($orderName.' '.$orderDesc)
	 					 ->page($nowPage.','.$pageNum)->select();
	 		$totalPage = ceil($count/$pageNum);
	 		C('web_admin_pagenum',$pageNum);
	 		//echo $model->getLastSql();exit();
	 		$del = L('cfg_list_itf_btn_delete');
	 		$upd = L('cfg_list_itf_btn_modify');
	 			
	 		$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
	 		foreach($list AS $row){
	 			$entry = array('id'=>$row['id'],
 					'cell'=>array(
 						'id'      => $row['id'],
 						'name'    => $row['name'],
 						'email'   => $row['email'],
 						'addtime' => date('Y-m-d',$row['addtime']),
 						'deal'    =>'
 						 <a href="#" class="btn red icn-only delAlone" title="'.$del.'" data-id="'.$row['id'].'"><i class="icon-remove"></i></a> 
 						 <a href="'.__URL__.'/email_sub_edit/id/'.$row['id'].'" class="btn blue icn-only" title="'.$upd.'"><i class="icon-edit"></i></a>'
 					),
	 			);
	 			$jsonData['rows'][] = $entry;
	 		}
	 		$this->ajaxReturn($jsonData,'JSON');
	 	}else{
	 		$this->display();
	 	}
	 }
	 
	/**
	 * 添加订阅者
	 */
	 public function email_sub_add(){
	 	 
	 	if(IS_POST){
	 
	 		$param = array(
 				"reply" => false,
 				"url"   => array("url"=>"Email/email_sub")
	 		);
	 		parent::do_add("EmailSubscribe",$param);
	 	}else{
	 	
	 		$this->display();
	 	}
	 }
	 
	/**
	 * 修改订阅者
	 */
	 public function email_sub_edit(){
	 	
	 	if(IS_POST){
	 	
	 		$param = array(
 				"reply" => false,
 				"url"   => array("url"=>"Email/email_sub"),
	 		);
	 		parent::do_edit("EmailSubscribe",$param);
	 	}else{
	 			
	 		$data   = D("EmailSubscribe");
	 		$id     = $this->_get("id","intval",0);
	 		$row    = $data->find($id);
	 		$this->assign("detail",$row);
	 		
	 		$this->display();
	 	}
	 }
	 
	/**
	 * 删除订阅者
	 * 物理删除
	 */ 
	 public function email_sub_del(){
	 	
	 	parent::do_del("EmailSubscribe");
	 }
	
	
}

?>